package com.news;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.graphics.Color;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

  private String               url           = "http://qeedi.com/m";

  private int                  webViewHeigth = 0, sWidth = 0, sHeight = 0,
      page = 1;

  public TextView              textView;

  public WebView               webView;

  public LinearLayout          linearLayout;

  public Button                btn1, btn2, btn3, btn4;

  private GestureDetector      gDetector;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    linearLayout = (LinearLayout) this.findViewById(R.id.WebviewContainer);
    webViewHeigth = Utils.getDisplayMetricsObj(this).heightPixels;
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        android.view.ViewGroup.LayoutParams.FILL_PARENT, webViewHeigth - 120);
    linearLayout.setLayoutParams(params);
    webView = (WebView) this.findViewById(R.id.Webview);

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    webView.setHorizontalScrollBarEnabled(false);
    webView.loadUrl(url);

    webView.setWebViewClient(new WebViewClient() {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
      }

      @Override
      public void onPageFinished(WebView view, String Url) {
        super.onPageFinished(view, Url);
        Log.d("URL", Url);
        MainActivity.this.setTitle(view.getTitle());

        if (Url.equalsIgnoreCase(url)) {
          page = 1;
        }
      }

    });

    webView.setWebChromeClient(new WebChromeClient() {
      @Override
      public void onProgressChanged(WebView view, int progress) {
        MainActivity.this.setTitle("Loading...");
        MainActivity.this.setProgress(progress * 100);
        if (progress == 100) {
          MainActivity.this.setTitle(view.getTitle());
        }
      }
    });
    
    
    // bottom buttons
    LinearLayout bottomBtns = (LinearLayout) findViewById(R.id.BottomBtns);
    bottomBtns.setBackgroundColor(Color.GRAY);

    sWidth = Utils.getScreenWidth(this);
    Log.d("ScreenWidth", "" + sWidth);
    btn1 = (Button) findViewById(R.id.ImageButton01);
    btn2 = (Button) findViewById(R.id.ImageButton02);
    btn3 = (Button) findViewById(R.id.ImageButton03);
    btn4 = (Button) findViewById(R.id.ImageButton04);
    int margin = (sWidth - 36 * 4) / 8;
    Log.d("Margin", "" + margin);
    LinearLayout.LayoutParams paramsBtn1 = (LinearLayout.LayoutParams) btn1
        .getLayoutParams();
    paramsBtn1.setMargins(margin, margin, margin, margin);
    btn1.setLayoutParams(paramsBtn1);

    LinearLayout.LayoutParams paramsBtn2 = (LinearLayout.LayoutParams) btn2
        .getLayoutParams();
    paramsBtn2.setMargins(margin, margin, margin, margin);
    btn2.setLayoutParams(paramsBtn2);

    LinearLayout.LayoutParams paramsBtn3 = (LinearLayout.LayoutParams) btn3
        .getLayoutParams();
    paramsBtn3.setMargins(margin, margin, margin, margin);
    btn3.setLayoutParams(paramsBtn3);

    LinearLayout.LayoutParams paramsBtn4 = (LinearLayout.LayoutParams) btn4
        .getLayoutParams();
    paramsBtn4.setMargins(margin, margin, margin, margin);
    btn4.setLayoutParams(paramsBtn4);

    gDetector = new GestureDetector(this.getApplicationContext(), new FlingGesture(this));
  }

  public void ClickBtn1(View view) {
    page -= 1;
    if (page < 1) {
      page = 1;
      Toast.makeText(this.getApplicationContext(), "已经是第一页了", 1).show();
    }
    else {
      webView.loadUrl(url + "/" + page);
    }
  }

  public void ClickBtn2(View view) {
    page = 1;
    String curUrl = webView.getUrl();
    Log.d("URL", curUrl);
    Log.d("URL", url);
    if (curUrl.equalsIgnoreCase(url)) {
      Toast.makeText(this.getApplicationContext(), "已经是首页了", 1).show();
    }
    else {
      webView.loadUrl(url);
    }
  }

  public void ClickBtn3(View view) {
    String url = webView.getUrl();
    webView.loadUrl(url);
  }

  public void ClickBtn4(View view) {
    page += 1;
    webView.loadUrl(url + "/" + page);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case R.id.NewsQuit:
        this.finish();
        break;
      case R.id.NewsHelp:
        webView.loadUrl("file:///android_asset/help.html");
        break;
      case R.id.NewsLogin:
          Intent login = new Intent();
          login.setClass(MainActivity.this, Login.class);
          this.startActivity(login);
          finish();
          break;
    }
    return true;
  }

  @Override
  public boolean onTouchEvent(MotionEvent me) {
    return gDetector.onTouchEvent(me);
  }

}
